// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyProcessAttributes.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>

// ****************************************************************************
// Module: PyProcessAttributes
//
// Purpose:
//   attributes to describe a running process
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a ProcessAttributes.
//
struct ProcessAttributesObject
{
    PyObject_HEAD
    ProcessAttributes *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewProcessAttributes(int);
std::string
PyProcessAttributes_ToString(const ProcessAttributes *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    {   const intVector &pids = atts->GetPids();
        snprintf(tmpStr, 1000, "%spids = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < pids.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%d", pids[i]);
            str += tmpStr;
            if(i < pids.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    {   const intVector &ppids = atts->GetPpids();
        snprintf(tmpStr, 1000, "%sppids = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < ppids.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%d", ppids[i]);
            str += tmpStr;
            if(i < ppids.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    {   const stringVector &hosts = atts->GetHosts();
        snprintf(tmpStr, 1000, "%shosts = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < hosts.size(); ++i)
        {
            snprintf(tmpStr, 1000, "\"%s\"", hosts[i].c_str());
            str += tmpStr;
            if(i < hosts.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    if(atts->GetIsParallel())
        snprintf(tmpStr, 1000, "%sisParallel = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%sisParallel = 0\n", prefix);
    str += tmpStr;
    {   const intVector &memory = atts->GetMemory();
        snprintf(tmpStr, 1000, "%smemory = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < memory.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%d", memory[i]);
            str += tmpStr;
            if(i < memory.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    {   const doubleVector &times = atts->GetTimes();
        snprintf(tmpStr, 1000, "%stimes = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < times.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%g", times[i]);
            str += tmpStr;
            if(i < times.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    return str;
}

static PyObject *
ProcessAttributes_Notify(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ProcessAttributes_SetPids(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;

    intVector vec;

    if (PyNumber_Check(args))
    {
        long val = PyLong_AsLong(args);
        int cval = int(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ int");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ int");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            long val = PyLong_AsLong(item);
            int cval = int(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ int", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ int", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more ints");

    obj->data->GetPids() = vec;
    // Mark the pids in the object as modified.
    obj->data->SelectPids();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ProcessAttributes_GetPids(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the pids.
    const intVector &pids = obj->data->GetPids();
    PyObject *retval = PyTuple_New(pids.size());
    for(size_t i = 0; i < pids.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyInt_FromLong(long(pids[i])));
    return retval;
}

/*static*/ PyObject *
ProcessAttributes_SetPpids(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;

    intVector vec;

    if (PyNumber_Check(args))
    {
        long val = PyLong_AsLong(args);
        int cval = int(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ int");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ int");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            long val = PyLong_AsLong(item);
            int cval = int(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ int", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ int", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more ints");

    obj->data->GetPpids() = vec;
    // Mark the ppids in the object as modified.
    obj->data->SelectPpids();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ProcessAttributes_GetPpids(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the ppids.
    const intVector &ppids = obj->data->GetPpids();
    PyObject *retval = PyTuple_New(ppids.size());
    for(size_t i = 0; i < ppids.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyInt_FromLong(long(ppids[i])));
    return retval;
}

/*static*/ PyObject *
ProcessAttributes_SetHosts(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;

    stringVector vec;

    if (PyUnicode_Check(args))
    {
        char const *val = PyUnicode_AsUTF8(args);
        std::string cval = std::string(val);
        if (val == 0 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ string");
        }
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyUnicode_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a unicode string", (int) i);
            }

            char const *val = PyUnicode_AsUTF8(item);
            std::string cval = std::string(val);

            if (val == 0 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ string", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more string(s)");

    obj->data->GetHosts() = vec;
    // Mark the hosts in the object as modified.
    obj->data->SelectHosts();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ProcessAttributes_GetHosts(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the hosts.
    const stringVector &hosts = obj->data->GetHosts();
    PyObject *retval = PyTuple_New(hosts.size());
    for(size_t i = 0; i < hosts.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyString_FromString(hosts[i].c_str()));
    return retval;
}

/*static*/ PyObject *
ProcessAttributes_SetIsParallel(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the isParallel in the object.
    obj->data->SetIsParallel(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ProcessAttributes_GetIsParallel(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetIsParallel()?1L:0L);
    return retval;
}

/*static*/ PyObject *
ProcessAttributes_SetMemory(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;

    intVector vec;

    if (PyNumber_Check(args))
    {
        long val = PyLong_AsLong(args);
        int cval = int(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ int");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ int");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            long val = PyLong_AsLong(item);
            int cval = int(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ int", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ int", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more ints");

    obj->data->GetMemory() = vec;
    // Mark the memory in the object as modified.
    obj->data->SelectMemory();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ProcessAttributes_GetMemory(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the memory.
    const intVector &memory = obj->data->GetMemory();
    PyObject *retval = PyTuple_New(memory.size());
    for(size_t i = 0; i < memory.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyInt_FromLong(long(memory[i])));
    return retval;
}

/*static*/ PyObject *
ProcessAttributes_SetTimes(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;

    doubleVector vec;

    if (PyNumber_Check(args))
    {
        double val = PyFloat_AsDouble(args);
        double cval = double(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ double");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ double");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            double val = PyFloat_AsDouble(item);
            double cval = double(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ double", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ double", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more doubles");

    obj->data->GetTimes() = vec;
    // Mark the times in the object as modified.
    obj->data->SelectTimes();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ProcessAttributes_GetTimes(PyObject *self, PyObject *args)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the times.
    const doubleVector &times = obj->data->GetTimes();
    PyObject *retval = PyTuple_New(times.size());
    for(size_t i = 0; i < times.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyFloat_FromDouble(times[i]));
    return retval;
}



PyMethodDef PyProcessAttributes_methods[PROCESSATTRIBUTES_NMETH] = {
    {"Notify", ProcessAttributes_Notify, METH_VARARGS},
    {"SetPids", ProcessAttributes_SetPids, METH_VARARGS},
    {"GetPids", ProcessAttributes_GetPids, METH_VARARGS},
    {"SetPpids", ProcessAttributes_SetPpids, METH_VARARGS},
    {"GetPpids", ProcessAttributes_GetPpids, METH_VARARGS},
    {"SetHosts", ProcessAttributes_SetHosts, METH_VARARGS},
    {"GetHosts", ProcessAttributes_GetHosts, METH_VARARGS},
    {"SetIsParallel", ProcessAttributes_SetIsParallel, METH_VARARGS},
    {"GetIsParallel", ProcessAttributes_GetIsParallel, METH_VARARGS},
    {"SetMemory", ProcessAttributes_SetMemory, METH_VARARGS},
    {"GetMemory", ProcessAttributes_GetMemory, METH_VARARGS},
    {"SetTimes", ProcessAttributes_SetTimes, METH_VARARGS},
    {"GetTimes", ProcessAttributes_GetTimes, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
ProcessAttributes_dealloc(PyObject *v)
{
   ProcessAttributesObject *obj = (ProcessAttributesObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *ProcessAttributes_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyProcessAttributes_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "pids") == 0)
        return ProcessAttributes_GetPids(self, NULL);
    if(strcmp(name, "ppids") == 0)
        return ProcessAttributes_GetPpids(self, NULL);
    if(strcmp(name, "hosts") == 0)
        return ProcessAttributes_GetHosts(self, NULL);
    if(strcmp(name, "isParallel") == 0)
        return ProcessAttributes_GetIsParallel(self, NULL);
    if(strcmp(name, "memory") == 0)
        return ProcessAttributes_GetMemory(self, NULL);
    if(strcmp(name, "times") == 0)
        return ProcessAttributes_GetTimes(self, NULL);


    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyProcessAttributes_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyProcessAttributes_methods[i].ml_name),
                PyString_FromString(PyProcessAttributes_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyProcessAttributes_methods, self, name);
}

int
PyProcessAttributes_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "pids") == 0)
        obj = ProcessAttributes_SetPids(self, args);
    else if(strcmp(name, "ppids") == 0)
        obj = ProcessAttributes_SetPpids(self, args);
    else if(strcmp(name, "hosts") == 0)
        obj = ProcessAttributes_SetHosts(self, args);
    else if(strcmp(name, "isParallel") == 0)
        obj = ProcessAttributes_SetIsParallel(self, args);
    else if(strcmp(name, "memory") == 0)
        obj = ProcessAttributes_SetMemory(self, args);
    else if(strcmp(name, "times") == 0)
        obj = ProcessAttributes_SetTimes(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
ProcessAttributes_print(PyObject *v, FILE *fp, int flags)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)v;
    fprintf(fp, "%s", PyProcessAttributes_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
ProcessAttributes_str(PyObject *v)
{
    ProcessAttributesObject *obj = (ProcessAttributesObject *)v;
    return PyString_FromString(PyProcessAttributes_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *ProcessAttributes_Purpose = "attributes to describe a running process";
#else
static char *ProcessAttributes_Purpose = "attributes to describe a running process";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(ProcessAttributesType,         \
                  "ProcessAttributes",           \
                  ProcessAttributesObject,       \
                  ProcessAttributes_dealloc,     \
                  ProcessAttributes_print,       \
                  PyProcessAttributes_getattr,   \
                  PyProcessAttributes_setattr,   \
                  ProcessAttributes_str,         \
                  ProcessAttributes_Purpose,     \
                  ProcessAttributes_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
ProcessAttributes_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &ProcessAttributesType
         || Py_TYPE(other) != &ProcessAttributesType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    ProcessAttributes *a = ((ProcessAttributesObject *)self)->data;
    ProcessAttributes *b = ((ProcessAttributesObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static ProcessAttributes *defaultAtts = 0;
static ProcessAttributes *currentAtts = 0;

static PyObject *
NewProcessAttributes(int useCurrent)
{
    ProcessAttributesObject *newObject;
    newObject = PyObject_NEW(ProcessAttributesObject, &ProcessAttributesType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new ProcessAttributes(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new ProcessAttributes(*defaultAtts);
    else
        newObject->data = new ProcessAttributes;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapProcessAttributes(const ProcessAttributes *attr)
{
    ProcessAttributesObject *newObject;
    newObject = PyObject_NEW(ProcessAttributesObject, &ProcessAttributesType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (ProcessAttributes *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
ProcessAttributes_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewProcessAttributes(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef ProcessAttributesMethods[] = {
    {"ProcessAttributes", ProcessAttributes_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *ProcessAttributesObserver = 0;

std::string
PyProcessAttributes_GetLogString()
{
    std::string s("ProcessAtts = ProcessAttributes()\n");
    if(currentAtts != 0)
        s += PyProcessAttributes_ToString(currentAtts, "ProcessAtts.", true);
    return s;
}

static void
PyProcessAttributes_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("ProcessAtts = ProcessAttributes()\n");
        s += PyProcessAttributes_ToString(currentAtts, "ProcessAtts.", true);
        cb(s);
    }
}

void
PyProcessAttributes_StartUp(ProcessAttributes *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyProcessAttributes_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(ProcessAttributesObserver == 0)
    {
        ProcessAttributesObserver = new ObserverToCallback(subj,
            PyProcessAttributes_CallLogRoutine, (void *)data);
    }

}

void
PyProcessAttributes_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete ProcessAttributesObserver;
    ProcessAttributesObserver = 0;
}

PyMethodDef *
PyProcessAttributes_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return ProcessAttributesMethods;
}

bool
PyProcessAttributes_Check(PyObject *obj)
{
    return (obj->ob_type == &ProcessAttributesType);
}

ProcessAttributes *
PyProcessAttributes_FromPyObject(PyObject *obj)
{
    ProcessAttributesObject *obj2 = (ProcessAttributesObject *)obj;
    return obj2->data;
}

PyObject *
PyProcessAttributes_New()
{
    return NewProcessAttributes(0);
}

PyObject *
PyProcessAttributes_Wrap(const ProcessAttributes *attr)
{
    return WrapProcessAttributes(attr);
}

void
PyProcessAttributes_SetParent(PyObject *obj, PyObject *parent)
{
    ProcessAttributesObject *obj2 = (ProcessAttributesObject *)obj;
    obj2->parent = parent;
}

void
PyProcessAttributes_SetDefaults(const ProcessAttributes *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new ProcessAttributes(*atts);
}

